#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年2月17日

@author: Administrator
'''
from account.models import Group
from common.models.abstract import TimeModel, StateModel
from django.contrib.auth.models import User
from django.db import models
from icon.models import Icon

class Resource(TimeModel, StateModel):
    name = models.CharField(default=u"", max_length=32, verbose_name=u"资源名称")
    icon = models.ForeignKey(Icon, null=True, blank=True, verbose_name=u"图标")
    parent = models.ForeignKey("self", null=True, blank=True, verbose_name=u"父节点资源")
    url = models.CharField(default=u"javascript:void(0);", max_length=512, verbose_name="路径")
    remark = models.CharField(default="", max_length=256, verbose_name=u"备注")
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        app_label = "funcresource"
        verbose_name = u"资源"
        verbose_name_plural = u"资源"
        ordering = ["-id"]
        
class ResourceGroup(TimeModel, StateModel):
    resource = models.ForeignKey(Resource, verbose_name=u"资源")
    group = models.ForeignKey(Group, verbose_name=u"所属分组")

    class Meta:
        app_label = "funcresource"
        verbose_name = u"资源分组"
        verbose_name_plural = u"资源分组"
        ordering = ["-id"]

class UserResource(TimeModel, StateModel):
    user = models.ForeignKey(User, verbose_name=u"用户")
    resource = models.ForeignKey(Resource, verbose_name=u"资源")
    
    class Meta:
        app_label = "funcresource"
        verbose_name = u"用户资源"
        verbose_name_plural = u"用户资源"
        unique_together = ("user", "resource")

class FuncResource(StateModel, TimeModel):
    name = models.CharField(default="", max_length=16, verbose_name=u"名称")
    url = models.CharField(default="javascript:viod(0)", unique=True, max_length=128, verbose_name=u"方法")
    remark = models.CharField(default="", max_length=256, verbose_name=u"备注")
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        app_label = "funcresource"
        verbose_name = u'功能资源'
        ordering = ["-id"]
        verbose_name_plural = u'功能资源'

class Functional(StateModel, TimeModel):
    func_resource = models.ForeignKey(FuncResource, verbose_name=u"功能资源")
    name = models.CharField(default="", max_length=16, verbose_name=u"名称")
    icon = models.ForeignKey(Icon, null=True, blank=True, verbose_name=u"图标")
    label_css = models.CharField(default="", max_length=128, verbose_name=u"标签css")
    remark = models.CharField(default="", max_length=256, verbose_name=u"备注")
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        app_label = "funcresource"
        verbose_name = u'功能'
        verbose_name_plural = u'功能'
        ordering = ["-id"]
        unique_together = ("func_resource", "label_css")

class UserFunctional(StateModel, TimeModel):
    user = models.ForeignKey(User, verbose_name=u"用户")
    func = models.ForeignKey(Functional, verbose_name=u"功能")
            
    class Meta:
        app_label = "funcresource"
        verbose_name = u'用户功能资源'
        verbose_name_plural = u'用户功能资源'
        ordering = ["-id"]
        unique_together = ("user", "func")




